package com.wx.network.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * 对于服务器传递过来response进行解码
 */
public class RpcClientDecoder extends ReplayingDecoder<RpcProtocol> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        int length = byteBuf.readInt();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        //转换为RpcProtocol传递给下一个handler
        RpcProtocol rpcProtocol = new RpcProtocol();
        rpcProtocol.setData(bytes);
        rpcProtocol.setLength(length);
        //移交handler处理
        list.add(rpcProtocol);
    }
}
